﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace BizWeb.Core.Extensions
{
    public interface IRegularExpressions
    {
        Regex GetExpression(string expressionName);
        bool IsMatch(string expressionName, string input);
        string Clean(string expressionName, string input);
    }

    public static class IRegularExpressionsExtensions
    {
        public static string GenerateSlug(this IRegularExpressions expression, string value)
        {
            string slug = "";

            if (!string.IsNullOrEmpty(value))
            {
                Regex regex = new Regex("([^a-z0-9-_]?)", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                slug = value.Trim();
                slug = slug.Replace(' ', '-');
                slug = slug.Replace("---", "-");
                slug = slug.Replace("--", "-");

                slug = regex.Replace(slug, "");

                if (slug.Length * 2 < value.Length)
                {
                    return "";
                }

                if (slug.Length > 100)
                {
                    slug = slug.Substring(0, 100);
                }
            }

            return slug;
        }

        public static string StripHtml(this IRegularExpressions expression, string value)
        {
            string stripped = "";

            if (!string.IsNullOrEmpty(value))
            {
                Regex regex = new Regex(@"<(.|\n)*?>", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);

                stripped = value.Trim();
                stripped = regex.Replace(stripped, "");
            }

            return stripped;
        }
   
    }
}
